<template>
    <div>
        <el-dialog v-model="dialogFormVisible" title="条件选择" width="500">
            <el-form :model="exportForm" :rules="rules" ref="ruleFormRef">
                <el-form-item label="渠道来源:" prop="qdly" :label-width="100">
                    <el-select v-model="exportForm.qdly" placeholder="请选择" @change="onChangeqdly">
                        <el-option v-for="(ele, index) in queryOpenAccountList" :key="ele.id" :label="ele.companyName"
                            :value="ele.id" />
                    </el-select>
                </el-form-item>
                <el-form-item label="产品类型:" :label-width="100">
                    <el-radio-group v-model="exportForm.cplx" :disabled="exportForm.qdly !== '-1'"
                        @change="changeproducttypeInDia">
                        <el-radio value="1">日日保</el-radio>
                        <el-radio value="2">长期保</el-radio>
                        <el-radio value="3">团意保</el-radio>
                    </el-radio-group>
                </el-form-item>
                <div v-if="exportForm.qdly === '-1'">
                    <el-form-item label="保司:" :label-width="100">
                        <el-select v-model="exportForm.bs" clearable placeholder="请选择">
                            <el-option v-for="(ele, index) in baosilistDia" :key="ele.dictValue" :label="ele.dictLabel"
                                :value="ele.dictValue" />
                        </el-select>
                    </el-form-item>
                    <el-form-item v-if="exportForm.cplx !== '3'" label="渠道:" :label-width="100">
                        <el-select v-model="exportForm.qd" clearable placeholder="请选择">
                            <el-option v-for="item in ditchoptions" :key="item.name" :label="item.name"
                                :value="item.id" />
                        </el-select>
                    </el-form-item>
                    <el-form-item v-if="exportForm.cplx !== '3'" prop="month" label="选择月份:" :label-width="100">
                        <el-date-picker clearable v-model="exportForm.month" class="w-full" value-format="YYYY-MM" type="month"
                            placeholder="请选择" />
                    </el-form-item>
                    <el-form-item label="所属销售:" :label-width="100">
                        <el-select v-model="exportForm.ssxs" clearable placeholder="请选择" filterable>
                            <el-option v-for="item in allseller" :key="item.saleId" :label="item.saleName"
                                :value="item.saleId" />
                        </el-select>
                    </el-form-item>
                </div>
                <div v-else>
                    <el-form-item label="选择月份:" prop="monthc" :label-width="100">
                        <el-date-picker clearable v-model="exportForm.monthc" class="w-full" value-format="YYYY-MM" type="month"
                            placeholder="请选择" />
                    </el-form-item>
                </div>
            </el-form>
            <template #footer>
                <div class="flex items-center w-full justify-center">
                    <el-button @click="diaExportCancel">取消</el-button>
                    <el-button type="primary" @click="diaExportSure(ruleFormRef)">
                        确定
                    </el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup lang="ts">
import { exportdaybxxdapi, exportlongbxxdapi, getditchapi, geteverydayinsuranceaudicompanytapi, getlongtimeinsuranceaudicompanytapi, getmarketapi, getQueryOpenAccountApi } from '@/api/api'
import type { QueryOpenAccountDto } from '@/api/api'
import baseurl from '@/api/env'
import { ElMessage, FormInstance, FormRules } from 'element-plus'
import { reactive, ref, watch } from 'vue'
const props = defineProps({
    show: {
        type: Boolean,
        default: false
    }
})
const rules = reactive<FormRules>({
    qdly: [
        {
            required: true,
            message: '请选择渠道来源',
            trigger: 'change',
        },
    ],
    month: [
        {
            required: true,
            message: '请选择月份',
            trigger: 'change',
        },
    ],
    monthc: [
        {
            required: true,
            message: '请选择月份',
            trigger: 'change',
        },
    ],
})
const ruleFormRef = ref<FormInstance>()
const exportForm = ref({
    qdly: '',
    cplx: '',
    bs: '',
    ssxs: '',
    qd: '',
    month: '',
    monthc: ''
})
const baosilistDia = ref<any>([])
const allseller = ref<any>([])
const ditchoptions = ref<any>([])
const emits = defineEmits(['update:show'])
const dialogFormVisible = ref(false)
const queryOpenAccountList = ref([] as QueryOpenAccountDto[])
// 获取渠道来源列表
const getQueryOpenAccount = async () => {
    queryOpenAccountList.value = await getQueryOpenAccountApi()
    queryOpenAccountList.value.unshift({ companyName: '系统', id: '-1' })
}
const getditch = () => {
    getditchapi().then((res: any) => {
        ditchoptions.value = res
    })
}
const getlongtimeinsuranceaudicompanyt = async () => {
    const res: any = await getlongtimeinsuranceaudicompanytapi()
    baosilistDia.value = res

}
const geteverydayinsuranceaudicompanyt = async () => {
    const res: any = await geteverydayinsuranceaudicompanytapi()
    baosilistDia.value = res
}
const getmarket = async () => {
    const res: any = await getmarketapi()
    allseller.value = res
}
const diaExportSure = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate(async (valid, fields) => {
        if (valid) {
            if (exportForm.value.cplx === '1') {
                exportdaybxxdapi({
                    queryMonth: exportForm.value.month,
                    insureCompanyType: exportForm.value.bs,
                    productType: 0,
                    channelId: exportForm.value.qd,
                    saleId: exportForm.value.ssxs,
                    companyId: exportForm.value.qdly === '-1' ? '' : exportForm.value.qdly
                }).then((res: any) => {
                    console.log(res)
                    window.location.href =
                        baseurl + '/common/download?delete=true&fileName=' + res
                    success()
                })
            } else if (exportForm.value.cplx === '2') {
                exportlongbxxdapi({
                    queryMonth: exportForm.value.month || exportForm.value.monthc,
                    insurCompany: exportForm.value.bs,
                    channelId: exportForm.value.qd,
                    saleId: exportForm.value.ssxs,
                    companyId: exportForm.value.qdly === '-1' ? '' : exportForm.value.qdly
                }).then((res: any) => {
                    console.log(res)
                    window.location.href =
                        baseurl + '/common/download?delete=true&fileName=' + res
                    success()
                })
            } else if (exportForm.value.cplx === '3') {
                exportdaybxxdapi({
                    queryMonth: exportForm.value.month,
                    insureCompanyType: exportForm.value.bs,
                    productType: 1,
                    channelId: exportForm.value.qd,
                    saleId: exportForm.value.ssxs,
                    companyId: exportForm.value.qdly === '-1' ? '' : exportForm.value.qdly
                }).then((res: any) => {
                    console.log(res)
                    window.location.href =
                        baseurl + '/common/download?delete=true&fileName=' + res
                    success()
                })
            }
            await diaExportCancel()
        } else {
            console.log('error submit!', fields)
        }
    })
}
const success = async () => {
    await ElMessage({
        message: '导出成功',
        type: 'success',
    })
}
const diaExportCancel = async () => {
    exportForm.value.qdly = '-1'
    exportForm.value.cplx = '1'
    exportForm.value.bs = ''
    exportForm.value.qd = ''
    exportForm.value.month = ''
    exportForm.value.monthc = ''
    exportForm.value.ssxs = ''
    await geteverydayinsuranceaudicompanyt()
    dialogFormVisible.value = false
}
const changeproducttypeInDia = (val: any) => {
    exportForm.value.bs = ''
    if (val === '') {
        baosilistDia.value = []
    }
    if (val === '1') {
        geteverydayinsuranceaudicompanyt()
    }
    if (val === '2') {
        getlongtimeinsuranceaudicompanyt()
    }
    if (val === '3') {
        exportForm.value.month = ''
        exportForm.value.qd = ''
        geteverydayinsuranceaudicompanyt()
    }
}
const onChangeqdly = (val: any) => {
    if (val !== '-1') {
        exportForm.value.cplx = '2'
        exportForm.value.bs = ''
        exportForm.value.qd = ''
        exportForm.value.month = ''
        exportForm.value.monthc = ''
        exportForm.value.ssxs = ''
    }
}
watch(() => props.show, async (newval) => {
    if (newval) {
        await getQueryOpenAccount()
        await diaExportCancel()
        await getditch()
        await getmarket()
    }
    dialogFormVisible.value = newval
})
watch(dialogFormVisible, (newval) => {
    emits('update:show', newval)
    if (!newval) {
        ruleFormRef.value?.resetFields()
    }
})
</script>

<style scoped></style>